# Create your views here.
from django.http import HttpResponse
from mysite.humor.models import Humor,User,Comment
from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponseRedirect
import datetime,time
import string
from django.middleware.gzip import GZipMiddleware
gzip_middleware = GZipMiddleware()
class Tag():
	color=''
	text=''
def getcolor(i):
	if i==1:
		return '#854f61'
	elif i==2:
		return '#ec7000'
	elif i==3:
		return '#cc0000'
	elif i==4:
		return '#b36d00'
	else:
		return '#206cff'
		
def parsetag(tagstr):
#	tagstr='1:test;2:test2'
	tokens = tagstr.split(';')
	taglist=[]
	for token in tokens:		
		tag = Tag()
		tag.color=getcolor(token[0])
		tag.text=token[2:]
		taglist.append(tag)
	return taglist;
def checkuser(imei):
	u=User.objects.filter(imei=imei)
	cnt =u.count();
	if cnt==0:
		u=User()
		u.name='nobody'
		u.email='no@no.com'
		u.gender=1
		u.imei=imei
		u.date=datetime.datetime.now()
		u.save()
		return u
	else:
		return u[0]

def filterhtml(str):
	str=str.replace('&lt;','<').replace('&gt;','>').replace('&quot;','"')
	return str
#add a joke
def webaddjoke(request):
	h=Humor()
	h.title= request.POST['title']
	h.body= request.POST['body']
	h.tag= request.POST['tag']
	h.url=request.POST['url']
	h.html=request.POST['html']
	h.date=datetime.datetime.now()
	h.user=User.objects.get(id=1)
	h.like=0
	h.commentcnt=0
	h.save()
	return HttpResponseRedirect('/humor/websubmit')
#web index page;
def webindex(request):
#	checkuser(imei)
	l=Humor.objects.all().order_by('-date')[0:10]
	return gzip_middleware.process_response(request, render_to_response('Free_Blog/index.html',{'list':l,'page':'0'}))
#web joke page
def webview(request,url):
	h=get_object_or_404(Humor,url=url)
	commentlist= Comment.objects.filter(humor=h).order_by('-date')[0:10]
	for t_c in commentlist:
		str=t_c.body
	return gzip_middleware.process_response(request,render_to_response('Free_Blog/view.html',{'joke':h,'comments':commentlist}))
def websubmit(request):
	return gzip_middleware.process_response(request,render_to_response('Free_Blog/editjoke.html',{}))
def webcomment(request,hid):
	h=get_object_or_404(Humor,pk=hid)
	h.commentcnt=h.commentcnt+1
	h.save()
	u=User()
	u.name=request.POST['name']
	u.email=request.POST['email']
	u.date=datetime.datetime.now()
	u.gender=request.POST['choice']
	u.save()
	c=Comment()
	c.body=filterhtml(request.POST['body'])
	c.humor=h
	c.user=u
	c.date=datetime.datetime.now()
	c.save()
	commentlist= Comment.objects.filter(humor=h).order_by('-date')[0:10]
	return HttpResponseRedirect('/humor/webview/%s/#'%h.url)
#	return gzip_middleware.process_response(request,render_to_response('Free_Blog/view.html',{'joke':h,'comments':commentlist}))
#user debug interface
def users(request):
	l=User.objects.all()
	return gzip_middleware.process_response(request, render_to_response('humor/users.xml',{'list':l}))
#home page,retrun a list of 10 jokes ordered by time desc
def home(request,imei):
	checkuser(imei)
	size=Humor.objects.count()
	l=Humor.objects.all().order_by('-date')[0:10]
	return gzip_middleware.process_response(request, render_to_response('humor/jokes.xml',{'list':l,'size':size,'imei':imei}))

def refreshhome(request,imei):
	checkuser(imei)
	size=Humor.objects.count()
	l=Humor.objects.all().order_by('-date')[0:10]
	return gzip_middleware.process_response(request, render_to_response('humor/refreshjokes.xml',{'list':l,'size':size,'imei':imei}))
#load more jokes by page,and the page start with 0 ,return a list of 10 jokes ordered by time desc
def more(request,imei,page):
	size = 10;
	p = string.atoi(page)+1
	index1= p*size;
	index2= index1 +size;
	l=Humor.objects.all().order_by('-date')[index1:index2]
	size =l.count()
	return gzip_middleware.process_response(request, render_to_response('humor/jokes.xml',{'list':l,'page':p,'size':size})) 

def morebyid(request,imei,hid):
	size=Humor.objects.count()
	l= Humor.objects.all().order_by('-date').filter(id__lt=hid)[:2]
	return gzip_middleware.process_response(request, render_to_response('humor/jokes.xml',{'list':l,'size':size})) 
def sync(request,imei):
	return HttpResponse('sync')
def view(request,imei,url):
	h=get_object_or_404(Humor,url=url)
	commentlist= Comment.objects.filter(humor=h).order_by('-date')[0:10]
	return gzip_middleware.process_response(request,render_to_response('Free_Blog/viewmob.html',{'humor':h,'comments':commentlist}))
#	return gzip_middleware.process_response(request,render_to_response('humor/%s.html'%url,{'humor':h,'comments':commentlist}))
    
def like(request,imei,hid):
	h=get_object_or_404(Humor,pk=hid)
	h.like+=1
	h.save()
	return gzip_middleware.process_response(request,render_to_response('humor/joke.xml',{'joke':h}))
def dislike(request,hid):
	h=get_object_or_404(Humor,pk=hid)
	h.dislike+=1
	h.save()
	return HttpResponse(h.dislike)
def old(request,hid):
	h=get_object_or_404(Humor,pk=hid)
	h.old+=1
	h.save()
	return HttpResponse(h.old)
#commit comment ,return 1 if suceed;
def postcomment(request,imei,hid,username,body):
	h=get_object_or_404(Humor,pk=hid)
	u=get_object_or_404(User,imei=imei)
	if u.name!=username:
		u.name =username
		u.save()
	c=Comment()
	c.body=body
	c.humor=h
	c.user=u
	c.date=datetime.datetime.now()
	c.save()
	h.commentcnt=h.commentcnt+1
	h.save()
	commentlist= Comment.objects.filter(humor=h).order_by('-date')[0:10]
	return gzip_middleware.process_response(request,render_to_response('Free_Blog/viewmob.html',{'humor':h,'comments':commentlist}))
#	return gzip_middleware.process_response(request,render_to_response('humor/%s.html'%h.url,{'humor':h,'comments':commentlist}))

def postcomment2(request,imei,hid):
	h=get_object_or_404(Humor,pk=hid)
	u=get_object_or_404(User,imei=imei)
	username=request.POST['name']
	if u.name!=username:
		u.name =username
		u.save()
	c=Comment()
	c.body=request.POST['body']
	c.humor=h
	c.user=u
	c.date=datetime.datetime.now()
	c.save()
	h.commentcnt=h.commentcnt+1
	h.save()
	commentlist= Comment.objects.filter(humor=h).order_by('-date')[0:10]
	return gzip_middleware.process_response(request,render_to_response('humor/comments.xml',{'list':commentlist}))
	
#view comments of the post by page.start page from page 0 renturn list of comment,size 10
def viewcomment(request,imei,hid,page):
	size = 10;
	p = string.atoi(page)
	index1= p*size;
	index2= index1 +size;
	p=p+1
	h =Humor.objects.get(pk=hid)
	l=Comment.objects.filter(humor=h).order_by('-date')[index1:index2]
	return gzip_middleware.process_response(request, render_to_response('humor/comments.xml',{'list':l,'page':p}))
def changeusername(request,imei,name):
	#check user name
	t_u =User.objects.filter(name__exact=name)
	cnt = t_u.count()
	res='0'
	#the user name is taken
	u=get_object_or_404(User,imei=imei)
	if cnt==0:	
		#set user name	
		u.name=name
		u.save()
		res='1'
	return gzip_middleware.process_response(request, render_to_response('humor/user.xml',{'user':u,'res':res}))
def gen(request):
	#gen a test user
 	cnt = User.objects.count()
	if cnt==0:
		u=User()
		u.name='tester'		
		u.date=datetime.datetime.now()
		u.email='test@tester.com'
		u.gender=0
		u.save()
	for i in range(1,10):
		h=Humor()
		h.title='the %s humor title.'%i
		h.body='the %s humor body.'%i
		h.tag='the %s humor tag.'%i
		h.url='the %s humor url.'%i
		h.date=datetime.datetime.now()
		h.user=User.objects.get(id=1)
		h.like=0
#		h.dislike=0
#		h.old=0
		h.save()
	return HttpResponse('gen test data done')

